package Email.auto.checker;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;

public class Tray{

	private ImageIcon icon = null; // Icon for menu items
	private TrayIcon trayicon = null;
	private SystemTray tray = null;
	private JPopupMenu popup = null;
	private JMenuItem menuItemEmailInformation = null;
	private JMenuItem menuItemVerifyMessg = null;
	private JMenuItem menuItemTimeout = null;
	private JMenuItem menuItemInsertEmail = null;
	private JMenuItem menuItemRemoveEmail = null;
	private JMenuItem menuItemHide = null;
	private JMenuItem menuItemExit = null;
	private Tray context = null;
	private Comunication comunication;
	private Account account = null;

	public Tray(Comunication com) {
		
		comunication = com;
		context = this;
		createObjects(); // Create all main Object
		displayObjects(); // Display all objects created

	}

	private void createObjects() {

		trayicon = new TrayIcon(createImage("images/iconTray.gif",
				"Email AutoChecker"), "Email AutoChecker", null); 
		trayicon.addMouseListener(new TrayListener(this));
		tray = SystemTray.getSystemTray();
		popup = new JPopupMenu();

		menuItemEmailInformation = new JMenuItem("No Account");
		menuItemEmailInformation.setEnabled(false);

		icon = new ImageIcon(getClass().getResource("images/verify.png"));
		menuItemVerifyMessg = new JMenuItem("Verify Unread Messages", icon);
		menuItemVerifyMessg.setEnabled(false);
		
		icon = new ImageIcon(getClass().getResource("images/timeout.png"));
		menuItemTimeout = new JMenuItem("Set Timeout", icon);
		menuItemTimeout.setEnabled(false);
		
		icon = new ImageIcon(getClass().getResource("images/add.png"));

		menuItemInsertEmail = new JMenuItem("Insert Email", icon);
		menuItemInsertEmail.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// Quando se cria o account faz-se logo a a comunicacao com o
				account = new Account(context, comunication);
			}

		});

		icon = new ImageIcon(getClass().getResource("images/close.png"));
		menuItemRemoveEmail = new JMenuItem("Remove Email", icon);
		menuItemRemoveEmail.setEnabled(false);
		
		menuItemExit = new JMenuItem("Exit");
		menuItemExit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		
		menuItemHide = new JMenuItem("Hide");
		menuItemHide.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				popup.setVisible(false);
			}
		});

		
		menuItemRemoveEmail.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				JOptionPane.showMessageDialog(null,
						"Conta removida com sucesso");
				menuItemEmailInformation.setText("No Account");
				menuItemInsertEmail.setEnabled(true);
				menuItemRemoveEmail.setEnabled(false);
				menuItemTimeout.setEnabled(false);
				menuItemVerifyMessg.setEnabled(false);
				comunication.endComunication(account.getDomain());
				
			}
		});

		menuItemVerifyMessg.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.out.println("Unreaded: "+ comunication.getUnreadedMessages(account.getDomain()));
				
				new Notification("httlopes@gmail.com", 10);

			}
		});

	}

	public JMenuItem getMenuItemVerifyMessg() {
		return menuItemVerifyMessg;
	}

	public void setMenuItemVerifyMessg(JMenuItem menuItemVerifyMessg) {
		this.menuItemVerifyMessg = menuItemVerifyMessg;
	}

	public JMenuItem getMenuItemTimeout() {
		return menuItemTimeout;
	}

	public void setMenuItemTimeout(JMenuItem menuItemTimeout) {
		this.menuItemTimeout = menuItemTimeout;
	}

	public JMenuItem getMenuItemRemoveEmail() {
		return menuItemRemoveEmail;
	}

	public void setMenuItemRemoveEmail(JMenuItem menuItemRemoveEmail) {
		this.menuItemRemoveEmail = menuItemRemoveEmail;
	}

	private void displayObjects() {

		popup.add(menuItemEmailInformation);
		popup.addSeparator();
		popup.add(menuItemVerifyMessg);
		popup.add(menuItemTimeout);
		popup.addSeparator();
		popup.add(menuItemInsertEmail);
		popup.add(menuItemRemoveEmail);
		popup.addSeparator();
		popup.add(menuItemHide);
		popup.add(menuItemExit);

		try {
			tray.add(trayicon);
		} catch (AWTException e) {
			// new ModError("Cannot access SystemTray");
			System.err.println("Cannot Access System Tray: " + e.toString());
			System.exit(0);
		}

	}

	protected static Image createImage(String path, String description) {
		URL imageURL = Tray.class.getResource(path);

		if (imageURL == null) {
			System.err.println("Resource not found: " + path);
			return null;
		} else {
			return (new ImageIcon(imageURL, description)).getImage();
		}
	}

	public SystemTray getTray() {
		return tray;
	}

	public void setTray(SystemTray tray) {
		this.tray = tray;
	}

	public JPopupMenu getPopup() {
		return popup;
	}

	public void setPopup(JPopupMenu popup) {
		this.popup = popup;
	}

	public JMenuItem getMenuItemEmailInformation() {
		return menuItemEmailInformation;
	}

	public void setMenuItemEmailInformation(JMenuItem menuItemEmailInformation) {
		this.menuItemEmailInformation = menuItemEmailInformation;
	}

	public JMenuItem getMenuItemInsertEmail() {
		return menuItemInsertEmail;
	}

	public void setMenuItemInsertEmail(JMenuItem menuItemInsertEmail) {
		this.menuItemInsertEmail = menuItemInsertEmail;
	}

}
